package com.csaba.swing.gui.util;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

import org.jdesktop.swingx.JXBusyLabel;

import com.csaba.swing.MainWindow;

@SuppressWarnings( "serial" )
public class BusyDialog extends JDialog
{
	private final JXBusyLabel busyLabel;
	private final JLabel messageLabel;
	private final JLabel titleLabel;

	//FIXME resolve blinking in tasks where multiple requests are received.
	public BusyDialog( final String title, final String message )
	{
		super();

		setTitle("Progress");
		setResizable(false);
		setUndecorated(true);
		setModal(true);

		busyLabel = new JXBusyLabel();
		titleLabel = new JLabel("<html><b><font size=3>" + title + "</font></b></html>");
		messageLabel = new JLabel(message);

		titleLabel.setBorder(BorderFactory.createEmptyBorder(20, 5, 10, 20));
		messageLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		busyLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 20));

		final JPanel messagePanel = new JPanel(new BorderLayout());
		messagePanel.add(titleLabel, BorderLayout.CENTER);
		messagePanel.add(messageLabel, BorderLayout.SOUTH);

		final JPanel content = (JPanel) getContentPane();
		content.setBorder(BorderFactory.createLineBorder(UIManager.getColor("desktop")));
		content.setLayout(new BorderLayout());
		content.add(busyLabel, BorderLayout.WEST);
		content.add(messagePanel, BorderLayout.CENTER);

		busyLabel.setBusy(true);
		pack();
		setLocationRelativeTo(MainWindow.getFrame());
	}

	public void setMessage( final String message )
	{
		messageLabel.setText(message);
		pack();
	}
}
